<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/7/17 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------


namespace app\common\subscribes;

use app\common\model\tang\Merchant;
use app\common\model\tang\Close;
use app\common\model\tang\NoReport;
use app\common\model\tang\Task;
use app\common\model\tang\TaskList;


class ReportSubscribe extends Base
{
    /**
     * 定时执行的方法
     */
    public function onReport()
    {
        //找出配置的任务列表
        $taskInfo = (new Task())->field('id,name,is_fixed,up_month,report_type,interval_day,start_time,end_time,task_time,is_audit')->select()->toArray();
        $time = time();//现在的时间
        $start = strtotime(date('Y-m-d ' . '00:10:00'));
        $end = strtotime(date('Y-m-d ' . '00:20:00'));
        if(time()>=$start && time()<=$end){
            foreach ($taskInfo as $key=>$val){
                if($val['is_fixed']==1){//说明是固定月上传
                    $nowMonth = date('m');
                    $taskFixed = explode(',',$val['up_month']);
                    foreach ($taskFixed as $k=>$v){
                        if((strtotime(date('Y-'.$v.'-01'))-86400)<=$time&&$nowMonth+1==$v){
                            $start_time =strtotime(date('Y-'.$v.'-01 00:00:00'));
                            $end_time = $start_time+$val['task_time'];
                            $merchant_sum = (new Merchant())->whereFindInSet('task_id',$val['id'])->count();
                            $taskList['task_id'] = $val['id'];
                            $taskList['start_time'] = $start_time;
                            $taskList['end_time'] = $end_time;
                            $taskList['merchant_sum'] = $merchant_sum;
                            $taskList['create_time'] = $time;
                            $isRepeat = (new TaskList())->where(['start_time'=>$start_time,'end_time'=>$end_time])->select()->toArray();
                            if(empty($isRepeat)){
                                (new TaskList())->save($taskList);
                            }
                        }
                    }
                }
                if(empty($val['is_fixed'])&&$val['interval_day']==1){//说明每天都需要上传
                    $str_time = explode(' ', $val['start_time']);
                    $start_time = strtotime(date('Y-m-d ' . $str_time[1]));
                    $end_time = $start_time + $val['task_time'];
                    $merchant_sum = (new Merchant())->whereFindInSet('task_id',$val['id'])->count();
                    $taskList['task_id'] = $val['id'];
                    $taskList['start_time'] = $start_time;
                    $taskList['end_time'] = $end_time;
                    $taskList['merchant_sum'] = $merchant_sum;
                    $taskList['create_time'] = $time;
                    $isRepeat = (new TaskList())->where(['start_time'=>$start_time,'end_time'=>$end_time])->select()->toArray();
                    if(empty($isRepeat)){
                        (new TaskList())->save($taskList);
                    }
                }
                if(empty($val['is_fixed'])&&$val['interval_day']>1){ //则此任务是间隔天数上传
                    //此处加86400是为了在任务开启前一天生成任务,并向商户推送消息
                    $sum = ((strtotime(date('Y-m-d 00:00:00',time()))+86400)-(strtotime($val['start_time'])+86400))/(86400*$val['interval_day']);
                    if(is_int($sum)) {//说明正好可以进行入任务
                        $start_time =strtotime(date('Y-m-d 00:00:00',time()))+86400;
                        $end_time = $start_time+$val['task_time'];
                        $merchant_sum = (new Merchant())->whereFindInSet('task_id',$val['id'])->count();
                        $taskList['task_id'] = $val['id'];
                        $taskList['start_time'] = $start_time;
                        $taskList['end_time'] = $end_time;
                        $taskList['merchant_sum'] = $merchant_sum;
                        $taskList['create_time'] = $time;
                        $isRepeat = (new TaskList())->where(['start_time'=>$start_time,'end_time'=>$end_time])->select()->toArray();
                        if(empty($isRepeat)){
                            (new TaskList())->save($taskList);
                        }
                    }
                }
            }
            //获取未统计的任务列表
            $taskMerchant = (new TaskList())->where('end_time','<',$time)->whereNull('up_sum')->whereNull('pass')->whereNull('nopass')->select()->toArray();
            foreach ($taskMerchant as $key=>$val){
                //已上报商户数
                $up_sum = (new Close())->where('type',$val['task_id'])->whereBetweenTime('create_time',$val['start_time'],$val['end_time'])->field('id,merchant_id,type,status')->select()->toArray();
                $merchant_sum = (new Merchant())->whereFindInSet('task_id',$val['id'])->field('id')->select()->toArray();
                $merchantArr = array_column($merchant_sum,'id');
                $upArr = array_column($up_sum,'merchant_id');
                $noReport = array_diff($merchantArr,$upArr);
                $noReportM = implode(',',$noReport);
                $pass = (new Close())->where(['type'=>$val['task_id'],'status'=>1])->whereBetweenTime('create_time',$val['start_time'],$val['end_time'])->count();
                $data['up_sum']  =count($up_sum);
                $data['no_report'] = $noReportM;
                $data['pass'] = $pass;
                $data['nopass'] =  (new Close())->where(['type'=>$val['task_id'],'status'=>2])->whereBetweenTime('create_time',$val['start_time'],$val['end_time'])->count();
                $taskModel = new TaskList();
                $taskModel->update($data,['id'=>$val['id']]);
            }
            //任务申报结束第二天,状态为审核失败的任务,变成可开罚单的状态
            (new Close())->where(['status'=>2,'is_ticket'=>0])->where('end_time','<',$time)->update(['is_ticket'=>1]);
        }



//        $start_time = strtotime(date('Y-m-d ' . '00:10:00'));
//        $end_time = strtotime(date('Y-m-d ' . '00:40:00'));
//        // 当前时间是否为 00：10 - 00:40
//        if(time()>=$start_time && time()<=$end_time){
//            // 查询所有商户
//            $Allmerchant = (new Merchant())->field("id,merchant_sn,name")->where('status', 1)->select()->toArray();
//
//            if (!empty($Allmerchant)) {
//                foreach ($Allmerchant as $key=> $item) {
//                    $Report = (new Close())->where('merchant_id',$item['id'])->whereTime('create_time','yesterday')->find();
//                    if(empty($Report)){
//                        $NoReport = (new NoReport())->where('merchant_id',$item['id'])->whereTime('create_time','today')->find();
//                        if(empty($NoReport)){
//                            $data = [
//                                'merchant_id'=>$item['id'],
//                                'merchant_name'=>$item['name'],
//                                'create_time'=>time()
//                            ];
//                            (new NoReport())->save($data);
//                        }
//
//                    }
//                }
//            }
//        }
    }

}